/* LoadListitemRenderer.java
*/
package org.imdm.portal.loadchecker.ui;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Label;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;
import org.zkoss.zul.RowRendererExt;
import org.zkoss.zul.Popup;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.Hbox;

/**
 * @author James.Sun
 *
 */
public class LoadRowRenderer implements RowRenderer,RowRendererExt{

	public void render(Row row, Object data) throws Exception {
		row.getChildren().clear();
		java.util.Map.Entry entry = (java.util.Map.Entry)data;
		LoadInfo li = (LoadInfo)entry.getValue();
		Label lcServer = new Label(li.getServer());
		Label lcLoad = new Label(li.getLoad());
		Label lcLinker = new Label(li.getLinker());
		Label lcLinkTime = new Label(li.getLinktime());
		String l = "..";
		if ( !li.getLoad().equalsIgnoreCase(LoadInfo._UPDATING) ) {
			if ( li.validLogins() )
				l = ""+li.getLogins()+" ";
			else
				l = LoadInfo._UNKNOWN;
		}
		Label lcLogins = new Label(l);
		Hbox hbLogins = new Hbox();
		hbLogins.appendChild(lcLogins);
		if ( li.validLogins() ) {
			hbLogins.appendChild(getLabelbyCount(li.getLogins()));
			Popup pl = this.getLoginPopup(li);
			if (pl != null) {
				pl.setParent(row.getGrid().getParent());
				hbLogins.setTooltip(pl.getId());
			}
		}

		//create popup tooltip for server name cells
		Popup ps = getServerPopup(li);
		ps.setParent(row.getGrid().getParent());
		lcServer.setTooltip(ps.getId());
		row.appendChild(lcServer);
		row.appendChild(lcLoad);
		row.appendChild(lcLinker);
		row.appendChild(lcLinkTime);
		row.appendChild(hbLogins);
	}

	private Popup getLoginPopup(LoadInfo li) {
		Popup p = new Popup();
		p.setWidth("26em");

		Label lb_svr = new Label();
		lb_svr.setMultiline(true);
		lb_svr.setPre(true);
		lb_svr.setStyle("font-weight:bold");
		lb_svr.setValue(li.getServer() + "\n ");

		Label lb_names = new Label();
		lb_names.setMultiline(true);
		lb_names.setPre(true);
		if ( li.getLogins() == 0 ) {
			lb_names.setValue("No user login.");
		}
		else {
			lb_names.setValue("" + li.getLogins() + " user" + ((li.getLogins()>1)?"s":"") 
							+ " with total " + li.getLogin_entities() + " login session"
							+ ((li.getLogin_entities()>1)?"s\n":"\n") + li.getLogin_names());
		}
		Vbox v = new Vbox();
		v.appendChild(lb_svr);
		v.appendChild(lb_names);
		p.appendChild(v);
		return p;
	}

	private Popup getServerPopup(LoadInfo li) {
		Popup p = new Popup();
		p.setWidth("26em");
		Vbox v = new Vbox();
		p.appendChild(v);
		Label lb_svr = new Label(li.getServer() + "\n ");
		lb_svr.setMultiline(true);
		lb_svr.setPre(true);
		lb_svr.setStyle("font-weight:bold");
		Label lb_info = new Label();
		lb_info.setMultiline(true);
		if ( li.getServerDetails().equalsIgnoreCase(LoadInfo._UNKNOWN) )
			lb_info.setValue(":-( "+li.getServer()+" is inaccessible !\nPlease contact MDM server prime !");
		else
			lb_info.setValue(li.getServerDetails());

		v.appendChild(lb_svr);
		v.appendChild(lb_info);
		return p;
	}
	
	private static Label getLabelbyCount(int i) {
		if ( i < 0 ) return new Label();
		StringBuffer _buf = new StringBuffer("_");
		for ( int n = 0; n < i; n++ ) {
			_buf.append("_");
		}
		Label _r = new Label(_buf.toString());
		_r.setStyle("color:#4343DD;background:#4343DD");
		if ( i == 1 )
			_r.setStyle("color:#00CC00;background:#00CC00");
		else if ( i > 1 && i <= 3)
			_r.setStyle("color:#DDCC00;background:#DDCC00");
		else if ( i > 3 && i <= 5)
			_r.setStyle("color:#FF8800;background:#FF8800");
		else if ( i > 5 )
			_r.setStyle("color:#FF3333;background:#FF3333");
		return _r;
	}

	public int getControls() {
		return RowRendererExt.DETACH_ON_UNLOAD;
	}

	public Component newCell(Row arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	public Row newRow(Grid arg0) {
		// TODO Auto-generated method stub
		return null;
	}
}
